<?php

/**
 * Questionaire form.
 *
 * @package    Attitude Orchestra
 * @subpackage form
 * @author     Attitude OÜ
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class QuestionaireForm extends BaseQuestionaireForm {

    public $languages = array();
    public static $multiLangFields = array('name' => array('title' => 'Name', 'class' => 'formInput'),
        'description' => array('title' => 'Description', 'class' => 'formTextarea'),
    );

    public function configure() {
        unset($this['created_at']);
        unset($this['updated_at']);
        unset($this['is_active']);
        unset($this['answered_ct']);
        
        $this->widgetSchema['structure_id'] =  new sfWidgetFormDoctrineChoice(array(
                                        'model' => $this->getRelatedModelName('Structure'), 
                                        'add_empty' => true,
                                        'query' => $this->getConNodesQuery()));

        $this->configureLanguages();
    }

    private function getConNodesQuery() {
        return Doctrine::getTable('Structure')->createQuery('q')->andWhere('q.ishidden = ?', 0)->andWhere('q.parameter = ?', 'clients');
    }

    public function configureLanguages() {

        $this->embedI18n($this->getLanguagesAbr());
        $this->languages = $this->getLanguages();


        foreach ($this->getLanguagesAbr() as $abr) {
            $this->widgetSchema[$abr]['description'] = new sfWidgetFormTextarea();
        }

        foreach (self::$multiLangFields as $title => $field)
            foreach ($this->getLanguagesAbr() as $abr)
                $this->widgetSchema[$abr][$title]->setAttribute('class', $field['class']);
    }

    public function getLanguagesAbr() {
        $languages = array();
        foreach ($this->getLanguages() as $lang) {
            $languages[] = $lang['abr'];
        }

        return $languages;
    }

    public function getLanguages() {
        return Doctrine::getTable('Language')->findAll();
    }

}
